package com.gopay.bis.manager.bank.cbrmb;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayBankDTOReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayBankDTOResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayEnrolmentCheckDTOReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayEnrolmentCheckDTOResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayOrderDTOReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayOrderDTOResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayOrderNotifyDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayPayerAuthValidateReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayPayerAuthValidateResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayQueryOrderDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayQueryOrderDTOReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayQueryOrderDTOResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayQueryRefundOrderDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayQueryRefundOrderDTOReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayQueryRefundOrderDTOResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayRefundOrderNotifyDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayUploadExpressDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayUploadExpressListDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayUploadExpressListResDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.RefundReqDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.RefundResDTO;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.cbrmb.masapay.MasaPayUtils;
import com.gopay.remote.bank.cbrmb.MasaPayRemoteService;

/**
 * 
 * @ClassName: MasaPayManagerImpl
 * @Description: TODO
 * @author gd.cui
 * @date 2017年12月4日 下午1:27:53
 */
public class MasaPayManagerImpl implements MasaPayManager {

	private static final Logger logger = LoggerFactory.getLogger(MasaPayManagerImpl.class);

	@Resource(name = "masaPayServiceClient")
	private MasaPayRemoteService masaPayServiceClient;

	private String signKey;
	// 商户id
	private String merchantId;

	@Override
	public MasaPayOrderDTOResp payOrder(MasaPayOrderDTOReq req) throws GopayException {
		// req.setMerchantId(merchantId);
		logger.info(String.format("masapay pay bis: %s", req.toString()));
		String signText = req.signatureData(signKey);
		logger.info(String.format("masapay pay bis-signText: %s", signText));
		String signMsg = MasaPayUtils.getSHA256Str(signText);
		logger.info(String.format("masapay pay bis-signMsg: %s", signMsg));
		req.setSignMsg(signMsg);
		MasaPayOrderDTOResp resp = masaPayServiceClient.payOrder(req);
		if (resp != null) {
			logger.info(String.format("masapay pay-resp bis: %s", resp.toString()));
			String respSignText = resp.signatureData(signKey);
			logger.info(String.format("masapay pay-resp bis-respSignText: %s", respSignText));
			String respSignMsg = MasaPayUtils.getSHA256Str(respSignText);
			logger.info(String.format("masapay pay-resp bis-respSignMsg: %s", respSignMsg));
			if (resp.getSignMsg().equalsIgnoreCase(respSignMsg)) {
				return resp;
			} else {
				return null;
			}
		}
		return resp;
	}

	@Override
	public RefundResDTO refundOrder(RefundReqDTO refundReqDTO) throws GopayException {
		// refundReqDTO.setMerchantId(merchantId);
		logger.info(String.format("masapay refund bis: %s", refundReqDTO.toString()));
		String signText = refundReqDTO.signatureData(signKey);
		logger.info(String.format("masapay refund bis-signText: %s", signText));
		String signMsg = MasaPayUtils.getSHA256Str(signText);
		logger.info(String.format("masapay refund bis-signMsg: %s", signMsg));
		refundReqDTO.setSignMsg(signMsg);
		RefundResDTO resp = masaPayServiceClient.refundOrder(refundReqDTO);
		if (resp != null) {
			logger.info(String.format("masapay refund-resp bis: %s", resp.toString()));
			String respSignText = resp.signatureData(signKey);
			logger.info(String.format("masapay refund-resp bis-respSignText: %s", respSignText));
			String respSignMsg = MasaPayUtils.getSHA256Str(respSignText);
			logger.info(String.format("masapay refund-resp bis-respSignMsg: %s", respSignMsg));
			if (resp.getSignMsg().equalsIgnoreCase(respSignMsg)) {
				return resp;
			} else {
				return null;
			}
		}
		return resp;
	}

	@Override
	public MasaPayQueryOrderDTOResp queryOrder(MasaPayQueryOrderDTOReq req) throws GopayException {
		// req.setMerchantId(merchantId);
		logger.info(String.format("masapay query bis: %s", req.toString()));
		String signText = req.signatureData(signKey);
		logger.info(String.format("masapay query bis-signText: %s", signText));
		String signMsg = MasaPayUtils.getSHA256Str(signText);
		logger.info(String.format("masapay query bis-signMsg: %s", signMsg));
		req.setSignMsg(signMsg);
		MasaPayQueryOrderDTOResp resp = masaPayServiceClient.queryOrder(req);
		if (resp != null) {
			logger.info(String.format("masapay query-resp bis: %s", resp.toString()));
			String respSignText = resp.signatureData(signKey);
			logger.info(String.format("masapay query-resp bis-respSignText: %s", respSignText));
			String respSignMsg = MasaPayUtils.getSHA256Str(respSignText);
			logger.info(String.format("masapay query-resp bis-respSignMsg: %s", respSignMsg));
			if (resp.getSignMsg().equalsIgnoreCase(respSignMsg) && resp.getList().size() > 0) {
				MasaPayQueryOrderDTO queryOrder = resp.getList().get(0);
				if (queryOrder != null) {
					String respOrderSignText = queryOrder.orderSignatureData(signKey);
					logger.info(String.format("masapay query-order-resp bis-respOrderSignText: %s", respOrderSignText));
					String respOrderSignMsg = MasaPayUtils.getSHA256Str(respOrderSignText);
					logger.info(String.format("masapay query-order-resp bis-respOrderSignMsg: %s", respOrderSignMsg));
					if (queryOrder.getOrdersignMsg().equalsIgnoreCase(respOrderSignMsg)) {
						return resp;
					}
				}
			}
		}
		return null;
	}

	@Override
	public boolean signOrderNotify(MasaPayOrderNotifyDTO orderNotify) {
		logger.info(String.format("masapay orderNotify bis: %s", orderNotify.toString()));
		String signText = orderNotify.signatureData(signKey);
		logger.info(String.format("masapay orderNotify bis-respSignText: %s", signText));
		String signMsg = MasaPayUtils.getSHA256Str(signText);
		logger.info(String.format("masapay orderNotify bis-respSignMsg: %s", signMsg));
		if (orderNotify.getSignMsg().equalsIgnoreCase(signMsg)) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean signRefundOrderNotify(MasaPayRefundOrderNotifyDTO refundOrderNotify) {
		logger.info(String.format("masapay refundOrderNotify bis: %s", refundOrderNotify.toString()));
		String signText = refundOrderNotify.signatureData(signKey);
		logger.info(String.format("masapay refundOrderNotify bis-respSignText: %s", signText));
		String signMsg = MasaPayUtils.getSHA256Str(signText);
		logger.info(String.format("masapay refundOrderNotify bis-respSignMsg: %s", signMsg));
		if (refundOrderNotify.getSignMsg().equalsIgnoreCase(signMsg)) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public MasaPayUploadExpressListResDTO uploadExpress(MasaPayUploadExpressListDTO req) throws GopayException {
		// req.setMerchantId(merchantId);
		logger.info(String.format("masapay uploadExpress bis: %s", req.toString()));
		String signText = req.signatureData(signKey);
		logger.info(String.format("masapay uploadExpress bis-signText: %s", signText));
		String signMsg = MasaPayUtils.getSHA256Str(signText);
		logger.info(String.format("masapay uploadExpress bis-signMsg: %s", signMsg));
		req.setSignMsg(signMsg);
		for (MasaPayUploadExpressDTO exp : req.getList()) {
			String expSignText = exp.signatureData(signKey);
			String expSignMsg = MasaPayUtils.getSHA256Str(expSignText);
			exp.setExpressSignMsg(expSignMsg);
		}

		MasaPayUploadExpressListResDTO resp = masaPayServiceClient.uploadExpress(req);
		if (resp != null) {
			logger.info(String.format("masapay uploadExpress-resp bis: %s", resp.toString()));
			String respSignText = resp.signatureData(signKey);
			logger.info(String.format("masapay uploadExpress-resp bis-respSignText: %s", respSignText));
			String respSignMsg = MasaPayUtils.getSHA256Str(respSignText);
			logger.info(String.format("masapay uploadExpress-resp bis-respSignMsg: %s", respSignMsg));
			if (resp.getSignMsg() != null && resp.getSignMsg().equalsIgnoreCase(respSignMsg)) {
				return resp;
			} else {
				return null;
			}
		}
		return resp;
	}

	@Override
	public MasaPayQueryRefundOrderDTOResp queryRefundOrder(MasaPayQueryRefundOrderDTOReq req) throws GopayException {
		// req.setMerchantId(merchantId);
		logger.info(String.format("masapay refund query bis: %s", req.toString()));
		String signText = req.signatureData(signKey);
		logger.info(String.format("masapay refund query bis-signText: %s", signText));
		String signMsg = MasaPayUtils.getSHA256Str(signText);
		logger.info(String.format("masapay refund query bis-signMsg: %s", signMsg));
		req.setSignMsg(signMsg);
		MasaPayQueryRefundOrderDTOResp resp = masaPayServiceClient.queryRefundOrder(req);
		if (resp != null) {
			logger.info(String.format("masapay query-resp bis: %s", resp.toString()));
			String respSignText = resp.signatureData(signKey);
			logger.info(String.format("masapay query-resp bis-respSignText: %s", respSignText));
			String respSignMsg = MasaPayUtils.getSHA256Str(respSignText);
			logger.info(String.format("masapay query-resp bis-respSignMsg: %s", respSignMsg));
			if (resp.getSignMsg().equalsIgnoreCase(respSignMsg) && resp.getList().size() > 0) {
				MasaPayQueryRefundOrderDTO queryOrder = resp.getList().get(0);
				if (queryOrder != null) {
					String respOrderSignText = queryOrder.orderSignatureData(signKey);
					logger.info(String.format("masapay query-order-resp bis-respOrderSignText: %s", respOrderSignText));
					String respOrderSignMsg = MasaPayUtils.getSHA256Str(respOrderSignText);
					logger.info(String.format("masapay query-order-resp bis-respOrderSignMsg: %s", respOrderSignMsg));
					if (queryOrder.getOrdersignMsg().equalsIgnoreCase(respOrderSignMsg)) {
						return resp;
					}
				}
				return resp;
			}
		}
		return null;
	}

	public String getMerchantId() {
		return merchantId;
	}

	public void setMerchantId(String merchantId) {
		this.merchantId = merchantId;
	}

	public String getSignKey() {
		return signKey;
	}

	public void setSignKey(String signKey) {
		this.signKey = signKey;
	}

	@Override
	public MasaPayEnrolmentCheckDTOResp enrolmentCheck(MasaPayEnrolmentCheckDTOReq req) throws GopayException {
		// req.setMerchantId(merchantId);
		logger.info(String.format("masapay enrolmentChecks: %s", req.toString()));
		String signText = req.signatureData(signKey);
		logger.info(String.format("masapay enrolmentCheck bis-signText: %s", signText));
		String signMsg = MasaPayUtils.getSHA256Str(signText);
		logger.info(String.format("masapay enrolmentCheck bis-signMsg: %s", signMsg));
		req.setSignMsg(signMsg);
		Long s=System.currentTimeMillis();
		logger.error("*************"+s);
		MasaPayEnrolmentCheckDTOResp resp = masaPayServiceClient.enrolmentCheck(req);
		logger.error(resp+"*************"+(System.currentTimeMillis()-s));
		if (resp != null) {
			logger.info(String.format("masapay enrolmentChecks-resp bis: %s", resp.toString()));
			String respSignText = resp.signatureData(signKey);
			logger.info(String.format("masapay enrolmentChecks-resp bis-respSignText: %s", respSignText));
			String respSignMsg = MasaPayUtils.getSHA256Str(respSignText);
			logger.info(String.format("masapay enrolmentChecks-resp bis-respSignMsg: %s", respSignMsg));
			if (resp.getSignMsg().equalsIgnoreCase(respSignMsg)) {
				return resp;
			} else {
				return null;
			}
		}
		return resp;
	}

	@Override
	public MasaPayBankDTOResp authenticationPayer(MasaPayBankDTOReq req) throws GopayException {
		MasaPayBankDTOResp resp = masaPayServiceClient.authenticationPayer(req);
		return resp;
	}

	@Override
	public MasaPayPayerAuthValidateResp payerAuthValidate(MasaPayPayerAuthValidateReq req) throws GopayException {
		// req.setMerchantId(merchantId);
		logger.info(String.format("masapay payerAuthValidate bis: %s", req.toString()));
		String signText = req.signatureData(signKey);
		logger.info(String.format("masapay payerAuthValidate bis-signText: %s", signText));
		String signMsg = MasaPayUtils.getSHA256Str(signText);
		logger.info(String.format("masapay payerAuthValidate bis-signMsg: %s", signMsg));
		req.setSignMsg(signMsg);
		MasaPayPayerAuthValidateResp resp = masaPayServiceClient.payerAuthValidate(req);
		if (resp != null) {
			logger.info(String.format("masapay payerAuthValidate-resp bis: %s", resp.toString()));
			String respSignText = resp.signatureData(signKey);
			logger.info(String.format("masapay payerAuthValidate-resp bis-respSignText: %s", respSignText));
			String respSignMsg = MasaPayUtils.getSHA256Str(respSignText);
			logger.info(String.format("masapay payerAuthValidate-resp bis-respSignMsg: %s", respSignMsg));
			if (resp.getSignMsg().equalsIgnoreCase(respSignMsg)) {
				return resp;
			} else {
				return null;
			}
		}
		return resp;
	}

}
